# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

pkgname=perl
pkgver=5.30.1
_gccver=9.1.0
pkgrel=1
pkgdesc="A highly capable, feature-rich programming language"
arch=(i686 x86_64)
license=('GPL')
url="https://www.perl.org/"
groups=('base-devel')
depends=('db' 'gdbm' 'libcrypt' 'coreutils' 'msys2-runtime' 'sh')
makedepends=('libdb-devel' 'libgdbm-devel' 'libcrypt-devel')
source=(https://www.cpan.org/src/5.0/perl-${pkgver}.tar.xz
        perlbin.sh
        perlbin.csh
        perlbin.fish
        perl-binary-module-dependency-1.template
        perl.cygwin-auto-image-base.patch
        perl.cygwin-hints.patch
        perl.cygwin-Configure-libpth.patch
        perl.cygwin-Configure-libsearch.patch
        perl.cygwin-Win32.patch
        perl-5.30.0-msys2.patch
        perl-cygwin-dont-add-fortify-source.patch
        regen-skip.diff)
options=('makeflags' '!purge' 'emptydirs')
# NOTE: This array is automatically generated by `./patchprov`.
#       If you want to add entries, do so in the next array.
provides=('perl-Archive-Tar=2.32'
          'perl-Attribute-Handlers=1.01'
          'perl-autodie=2.29'
          'perl-AutoLoader=5.74'
          'perl-autouse=1.11'
          'perl-base=2.27'
          'perl-bignum=0.51'
          'perl-Carp=1.50'
          'perl-Compress-Raw-Bzip2=2.084'
          'perl-Compress-Raw-Zlib=2.084'
          'perl-Config-Perl-V=0.32'
          'perl-constant=1.33'
          'perl-CPAN=2.22'
          'perl-CPAN-Meta=2.150010'
          'perl-CPAN-Meta-Requirements=2.140'
          'perl-CPAN-Meta-YAML=0.018'
          'perl-Data-Dumper=2.174'
          'perl-DB_File=1.843'
          'perl-Devel-PPPort=3.52'
          'perl-Devel-SelfStubber=1.06'
          'perl-Digest=1.17_01'
          'perl-Digest-MD5=2.55'
          'perl-Digest-SHA=6.02'
          'perl-Dumpvalue=1.18'
          'perl-Encode=3.01'
          'perl-encoding-warnings=0.13'
          'perl-Env=1.04'
          'perl-experimental=0.020'
          'perl-Exporter=5.73'
          'perl-ExtUtils-CBuilder=0.280231'
          'perl-ExtUtils-Constant=0.25'
          'perl-ExtUtils-Install=2.14'
          'perl-ExtUtils-MakeMaker=7.34'
          'perl-ExtUtils-Manifest=1.72'
          'perl-ExtUtils-ParseXS=3.40'
          'perl-File-Fetch=0.56'
          'perl-File-Path=2.16'
          'perl-File-Temp=0.2309'
          'perl-Filter-Simple=0.95'
          'perl-Filter-Util-Call=1.59'
          'perl-Getopt-Long=2.50'
          'perl-HTTP-Tiny=0.076'
          'perl-I18N-Collate=1.02'
          'perl-I18N-LangTags=0.43'
          'perl-if=0.0608'
          'perl-IO-Compress=2.084'
          'perl-IO-Socket-IP=0.39'
          'perl-IO-Zlib=1.10'
          'perl-IO=1.40'
          'perl-IPC-Cmd=1.02'
          'perl-IPC-SysV=2.07'
          'perl-JSON-PP=4.02'
          'perl-lib=0.65'
          'perl-libnet=3.11'
          'perl-Locale-Maketext-Simple=0.21_01'
          'perl-Locale-Maketext=1.29'
          'perl-Math-BigInt=1.999816'
          'perl-Math-BigInt-FastCalc=0.5008'
          'perl-Math-BigRat=0.2614'
          'perl-Math-Complex=1.59_01'
          'perl-Memoize=1.03_01'
          'perl-MIME-Base64=3.15'
          'perl-Module-CoreList=5.20191011'
          'perl-Module-Load=0.34'
          'perl-Module-Load-Conditional=0.68'
          'perl-Module-Loaded=0.08'
          'perl-Module-Metadata=1.000036'
          'perl-NEXT=0.67_01'
          'perl-Net-Ping=2.71'
          'perl-Params-Check=0.38'
          'perl-parent=0.237'
          'perl-Parse-CPAN-Meta=2.150010'
          'perl-PathTools=3.78'
          'perl-Perl-OSType=1.010'
          'perl-perlfaq=5.20190126'
          'perl-PerlIO-via-QuotedPrint=0.08'
          'perl-Pod-Checker=1.73'
          'perl-Pod-Escapes=1.07'
          'perl-Pod-Parser=1.63'
          'perl-Pod-Perldoc=3.2801'
          'perl-Pod-Simple=3.35'
          'perl-Pod-Usage=1.69'
          'perl-podlators=4.10'
          'perl-Safe=2.40'
          'perl-Scalar-List-Utils=1.50'
          'perl-Search-Dict=1.07'
          'perl-SelfLoader=1.25'
          'perl-Socket=2.027'
          'perl-Storable=3.15'
          'perl-Sys-Syslog=0.35'
          'perl-Term-ANSIColor=4.06'
          'perl-Term-Cap=1.17'
          'perl-Term-Complete=1.403'
          'perl-Term-ReadLine=1.17'
          'perl-Test-Harness=3.42'
          'perl-Test-Simple=1.302162'
          'perl-Test=1.31'
          'perl-Text-Abbrev=1.02'
          'perl-Text-Balanced=2.03'
          'perl-Text-ParseWords=3.30'
          'perl-Text-Tabs=2013.0523'
          'perl-Thread-Queue=3.13'
          'perl-Thread-Semaphore=2.13'
          'perl-threads-shared=1.60'
          'perl-threads=2.22'
          'perl-Tie-File=1.02'
          'perl-Tie-RefHash=1.39'
          'perl-Time-HiRes=1.9760'
          'perl-Time-Local=1.28'
          'perl-Time-Piece=1.33'
          'perl-Unicode-Collate=1.27'
          'perl-Unicode-Normalize=1.26'
          'perl-version=0.9924'
          'perl-XSLoader=0.30')
# Add your own provides here
provides=(${provides[@]})
sha256sums=('7336cd3ed0535eb61b76a71350effcfa7c88b44faf37d64d70952ced5d38cd35'
            '91bf8a45a3f04f51fe0eca146a71e278d41dbde19ba60dabc1e41c4639477c07'
            '478a52dc440d5ca324f4c95238b39cc8c90d5d7d8d5601fa085ffef79fea04f4'
            '11ce66bcf4803b0bf27179a32ce53f41bcdfa967c4c0d54ac103dbc4951d18a3'
            'fe91f1de38af525cb82503feef553b71299c52ea52c01b1959143f48f4051ffa'
            '9745883a27edc99ac0a90cbbf930c2a532085719dc1eaf801b2dda29f7d475e9'
            'ecc088b7c69676179bf149e53d558e49645e03aff33f041ce35ec353865bedcc'
            '2520356a2ffca851207a38adeb8fd0af4d87bcaa20c6ebf5185295ba588fac58'
            '48083033bce0d57d3b41d6febe3db824d00751b6f86f65380abc773837c23ede'
            'f2ce1f39bb98479ed024f9575794bb6a85f9c45d9fad40910a5adeec3b59a531'
            '886acf8359a364c53013e179054808ce9a1b1c3f97a89a06021068f21de6f9f0'
            'd2f17b92fa1a2178e5fa6f60c8275a16cfe26fc20d8f4030a018007137d4478a'
            'f2f28c33743f767e98a5c6b2666a838ed12590d913b02b2baf3258d6c175dc06')

prepare() {
  chmod -R 0666 ${srcdir}/${pkgname}-${pkgver}
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p2 -i ${srcdir}/perl.cygwin-auto-image-base.patch
  patch -p1 -i ${srcdir}/perl.cygwin-hints.patch
  patch -p2 -i ${srcdir}/perl.cygwin-Configure-libsearch.patch
  patch -p2 -i ${srcdir}/perl.cygwin-Configure-libpth.patch
  patch -p1 -i ${srcdir}/perl.cygwin-Win32.patch

  patch -p1 -i ${srcdir}/perl-5.30.0-msys2.patch
  patch -p1 -i ${srcdir}/perl-cygwin-dont-add-fortify-source.patch

  #debian
  patch -p1 -i ${srcdir}/regen-skip.diff

  cat > Policy.sh <<_EOF
#!/bin/sh
d_readlink='undef'
_EOF

sed -ri '/\s+$/s///' $(find . -type f | grep -vE ".aux$|.ico$|.tgz$|.tbz$|.tar$|.utf$|.enc$|.patch$|.pub$|.xml$|.t$|laozi38p.pod$|opnames.h$|perlguts.pod$|trailing-space$")

  # test broken with gdbm 1.15. See: https://rt.perl.org/Public/Bug/Display.html?id=133295
  sed -i 's|BEGIN {|BEGIN { plan(skip_all => "fatal test unsupported with gdbm 1.15");|' ext/GDBM_File/t/fatal.t

}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  ./Configure -des -Dusethreads \
    -Doptimize="${CFLAGS}" \
    -Dprefix=/usr \
    -Dvendorprefix=/usr \
    -Dprivlib=/usr/share/perl5/core_perl \
    -Darchlib=/usr/lib/perl5/core_perl \
    -Dsitelib=/usr/share/perl5/site_perl \
    -Dsitearch=/usr/lib/perl5/site_perl \
    -Dvendorlib=/usr/share/perl5/vendor_perl \
    -Dvendorarch=/usr/lib/perl5/vendor_perl \
    -Dscriptdir=/usr/bin/core_perl \
    -Dsitescript=/usr/bin/site_perl \
    -Dvendorscript=/usr/bin/vendor_perl \
    -Dinc_version_list=none \
    -Dman1ext=1perl -Dman3ext=3perl \
    -Darchname=${ARCH}-msys-threads \
    -Dmyarchname=${ARCH}-msys \
    -Dlibperl=msys-perl5_30.dll \
    -Dcc=gcc -Dld=g++ \
    -Accflags="$CFLAGS -fwrapv"

  LC_ALL=C make
}

check() {
  cd ${srcdir}/${pkgname}-${pkgver}
  TEST_JOBS=$(echo $MAKEFLAGS | sed 's/.*-j\([0-9][0-9]*\).*/\1/') make test_harness || true
  #make test
}

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make DESTDIR="${pkgdir}" V=1 install

  for template in "${srcdir}/"*.template; do
    install -Dm644 "${template}" "${pkgdir}/usr/share/makepkg-template/${template##*/}"
  done
  ln -s perl-binary-module-dependency-1.template "${pkgdir}/usr/share/makepkg-template/"perl-binary-module-dependency.template

  ### Perl Settings ###
  # Change man page extensions for site and vendor module builds.
  # Set no mail address since bug reports should go to the bug tracker
  # and not someone's email.
  sed -e '/^man1ext=/ s/1perl/1p/' -e '/^man3ext=/ s/3perl/3pm/' \
      -e "/^cf_email=/ s/'.*'/''/" \
      -e "/^perladmin=/ s/'.*'/''/" \
      -i ${pkgdir}/usr/lib/perl5/core_perl/Config_heavy.pl

  ### CPAN Settings ###
  # Set CPAN default config to use the site directories.
  sed -e '/(makepl_arg =>/   s/""/"INSTALLDIRS=site"/' \
      -e '/(mbuildpl_arg =>/ s/""/"installdirs=site"/' \
      -i ${pkgdir}/usr/share/perl5/core_perl/CPAN/FirstTime.pm

  # Profile script to set paths to perl scripts.
  install -D -m755 ${srcdir}/perlbin.sh \
                   ${pkgdir}/etc/profile.d/perlbin.sh
  # Profile script to set paths to perl scripts on csh. (FS#22441)
  install -D -m755 ${srcdir}/perlbin.csh \
                  ${pkgdir}/etc/profile.d/perlbin.csh
  # Profile script to set paths to perl scripts on fish. (FS#51191)
  install -D -m755 ${srcdir}/perlbin.fish \
                  ${pkgdir}/usr/share/fish/vendor_conf.d/perlbin.fish

  # Add the dirs so new installs will already have them in PATH once they
  # install their first perl programm
  install -d -m755 "${pkgdir}/usr/bin/vendor_perl"
  install -d -m755 "${pkgdir}/usr/bin/site_perl"

  #(cd ${pkgdir}/usr/bin; mv perl${pkgver} perl)

  # Remove all pod files *except* those under /usr/lib/perl5/core_perl/pods/
  # (FS#16488)
  rm -f ${pkgdir}/usr/share/perl5/core_perl/*.pod
  for d in ${pkgdir}/usr/share/perl5/core_perl/*; do
    if [ -d ${d} -a $(basename ${d}) != "pods" ]; then
      find ${d} -name *.pod -delete
    fi
  done
  find ${pkgdir}/usr/lib -name *.pod -delete
  find ${pkgdir} -name .packlist -delete

  find ${pkgdir}/usr/lib -type f -exec chmod 0644 {} \;
  find ${pkgdir}/usr/share/perl5 -type f -exec chmod 0644 {} \;
  # prevent xsinit-generated code from referencing boot_Win32CORE
  sed -i -e "s/^\(static_ext\)=.*/\1=' '/" \
    ${pkgdir}/usr/lib/perl5/core_perl/Config_heavy.pl

  install -D -m755 ${srcdir}/${pkgname}-${pkgver}/lib/auto/XS/APItest/APItest.dll \
                  ${pkgdir}/usr/lib/perl5/core_perl/auto/XS/APItest/APItest.dll
  install -D -m755 ${srcdir}/${pkgname}-${pkgver}/lib/auto/XS/Typemap/Typemap.dll \
                  ${pkgdir}/usr/lib/perl5/core_perl/auto/XS/Typemap/Typemap.dll
  install -D -m755 ${srcdir}/${pkgname}-${pkgver}/lib/auto/Win32CORE/Win32CORE.a \
                  ${pkgdir}/usr/lib/perl5/core_perl/auto/Win32CORE/Win32CORE.a

}
